<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
session_start();

class cpanel extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library("soap");
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->library('upload');
        $this->load->library('pagination');
        $this->load->helper(array('form', 'url'));
        @session_start();
    }

    public function index($nof = null) {
        if ($this->session->userdata('adminid_sms') == null) {
            redirect('cpanel/login');
        } else {
            $this->load->model("transms_model");
            $this->load->model("report_model");
            //Hien thi MO
            if (isset($_REQUEST['searchbtt'])) {
                $content = $this->input->post('contentsearch', true);
                $type = $this->input->post('typesearch', true);

                if ($content == null || $type == null) {
                    $data['shoutbox'] = 3;
                    $data['totalsms'] = 0;
                    $data['receivesmo'] = array();
                    $data['pages'] = array();
                } else {
                    $config['base_url'] = site_url('cpanel/index');
                    $config['total_rows'] = $this->transms_model->totalmo($content, $type);
                    $config['per_page'] = 50;
                    $config['prev_link'] = 'Đầu';
                    $config['next_link'] = 'Tiếp';
                    $this->pagination->initialize($config);
                    $data['receivesmo'] = $this->transms_model->show_mobytype($type, $content, $config['per_page'], $this->uri->segment(3));
                    $data['pages'] = $this->pagination->create_links();
                    $data['shoutbox'] = 0;
                    $data['totalsms'] = $this->transms_model->totalmo($content, $type);
                }
            } else {
                date_default_timezone_set('Asia/Bangkok');
                $config['base_url'] = site_url('cpanel/index');
                $config['total_rows'] = $this->transms_model->totalmotoday(date('d-m-Y'));
                $config['per_page'] = 50;
                $config['prev_link'] = 'Đầu';
                $config['next_link'] = 'Tiếp';
                $this->pagination->initialize($config);
                $data['receivesmo'] = $this->transms_model->show_mo($config['per_page'], $this->uri->segment(3));
                $data['pages'] = $this->pagination->create_links();
                $data['shoutbox'] = 0;
                $data['totalsms'] = $this->transms_model->totalmo();
                $data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
            }
            /*  ---- - - -- --REPORT - - - - - -   */
            $date = date('d-m-Y');
            $this->report_model->removeallreportday();
            for ($i = 0; $i <= 8; $i++) {
                $new_date = strtotime('-' . $i . ' day', strtotime($date));
                $new_date = date('d-m-Y', $new_date);
                $this->report_model->insert_report_day($this->report_model->report_for_week($new_date), $new_date);
            }

            $data['reportbydays'] = $this->report_model->get_report_day();
            $this->load->model('chatbox_model');
            $this->load->model('user_model');
            $config['base_url'] = site_url('webhome/');
            $config['total_rows'] = $this->chatbox_model->_totalchatbox();
            $config['per_page'] = 30;
            $data['chatbox'] = $this->chatbox_model->_showchatbox($config['per_page'], $this->uri->segment(2));
            $data['adminname'] = $this->session->userdata('adminid_sms');
            $data['adminid'] = $this->session->userdata('adminid_sms');


            $data['shoutbox'] = 0;
            $config['base_url'] = site_url('cpanel/usercp');
            $config['total_rows'] = $this->user_model->totaluser();
            $config['per_page'] = 30;
            $config['prev_link'] = 'Đầu';
            $config['next_link'] = 'Tiếp';
            $this->pagination->initialize($config);
            $data['listusers'] = $this->user_model->_listuser($config['per_page'], $this->uri->segment(3));
            $data['pages'] = $this->pagination->create_links();
            $data['totaluser'] = $this->user_model->totaluser();
            $data['total_member'] = $this->user_model->total_member();
            $this->load->view('appstore/admin/sms/dashboard', $data);
        }
    }

    public function login($err = null) {

        if (isset($_REQUEST['submit'])) {
            $username = $this->input->post('uname', true);
            $pass1 = $this->input->post('pass1', true);
            $this->load->model('admin_model');
            $result = $this->admin_model->checkuser($username, md5($pass1));
            if ($result == null) {
                redirect('cpanel/login/' . rand(1, 10)); //lá»—i sai username hoáº·c pass
            } else {
                $newdata = array(
                    'adminid_sms' => $result,
                    'adminname_sms' => $username
                );
                $this->session->set_userdata($newdata);
                redirect('cpanel/index');
            }
        }
        if ($err <> null) {
            $err = "<font color=red>Error Code: " . $err . '</font>';
        }
        $data['caution'] = $err;
        $data['shoutbox'] = 0;
        $this->load->view('appstore/admin/sms/login', $data);
    }

    public function logout() {
        $this->session->unset_userdata('adminid_sms');
        $this->session->unset_userdata('adminname_sms');
        $this->session->sess_destroy();
        redirect('cpanel');
    }

    public function searchtotalbymonth() {

        if ($this->session->userdata('adminid_sms') == null) {
            redirect('cpanel/login');
        } else {
            
                $this->load->model("transms_model");
                $this->load->model('report_model');
                $this->load->model('chatbox_model');
                $this->load->model('user_model');
                $this->load->model('transms_model');
                $data['totaluser'] = $this->user_model->totaluser();
                $data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
                $data['totalsms'] = $this->transms_model->totalmo();
                //--------------END Function ----------------------------------
                $thang = null;
                $nam = null;
                if (isset($_REQUEST['submit'])) {
                    $thang = $this->input->post("thangdate", true);
                    $nam = $this->input->post("namdate", true);
                    $data['values'] = $this->transms_model->totalmobymonth($thang, $nam);
                }
                $data['values'] = $this->transms_model->totalmobymonth($thang, $nam);
                $this->load->view('appstore/admin/sms/dashboard', $data);
        
        }
    }

    public function member() {

        if ($this->session->userdata('adminid_sms') == null) {
            redirect('cpanel/login');
        } else {
            //--------------Addition Function ----------------------------------
            $this->load->model("transms_model");
            $this->load->model('report_model');
            $this->load->model('chatbox_model');
            $this->load->model('user_model');
            $this->load->model('transms_model');
            $data['reportbydays'] = $this->report_model->get_report_day();
            $data['totaluser'] = $this->user_model->totaluser();
            $data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
            $data['totalsms'] = $this->transms_model->totalmo();
            //--------------Addition Function ---------------------------------- 
            $config['base_url'] = site_url('cpanel/member');
            $config['total_rows'] = $this->user_model->total_member();
            $config['per_page'] = 50;
            $config['prev_link'] = 'Last';
            $config['next_link'] = 'Next';
            $this->pagination->initialize($config);
            $data['members'] = $this->user_model->_listmember($config['per_page'], $this->uri->segment(3));
            $data['pages'] = $this->pagination->create_links();
            $data['shoutbox'] = 0;
            $data['total_member'] = $this->user_model->total_member();
            $this->load->view('appstore/admin/sms/dashboard', $data);
        }
    }

    public function checkmoservices($nof = null) {
        if ($this->session->userdata('adminid_sms') == null) {
            redirect('cpanel/login');
        } else {
            $this->load->model("transms_model");
            $this->load->model('report_model');
            $this->load->model('chatbox_model');
            $this->load->model('user_model');
            $this->load->model('transms_model');
            $data['reportbydays'] = $this->report_model->get_report_day();
            $data['totaluser'] = $this->user_model->totaluser();
            $data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
            $data['totalsms'] = $this->transms_model->totalmo();
            //--------------Addition Function ----------------------------------

            if (isset($_REQUEST['searchbtt'])) {
                $content = $this->input->post('contentsearch', true);
                $type = $this->input->post('typesearch', true);
                if ($content == null || $type == "none") {
                    redirect('cpanel/checkmoservices');
                }
                if ($content == null || $type == null) {
                    $data['shoutbox'] = 3;
                    $data['totalsms'] = 0;
                    $data['receivesmo'] = array();
                    $data['pages'] = array();
                } else {
                    $config['base_url'] = site_url('cpanel/index');
                    $config['total_rows'] = $this->transms_model->totalmo($content, $type);
                    $config['per_page'] = 50;
                    $config['prev_link'] = 'Đầu';
                    $config['next_link'] = 'Tiếp';
                    $this->pagination->initialize($config);
                    $data['receivesmo'] = $this->transms_model->show_mobytype($type, $content, $config['per_page'], $this->uri->segment(3));
                    $data['pages'] = $this->pagination->create_links();
                    $data['shoutbox'] = 0;
                    $data['totalsms'] = $this->transms_model->totalmo($content, $type);


                    $data['adminname'] = $this->session->userdata('adminid_sms');
                    $data['adminid'] = $this->session->userdata('adminid');
                    $this->load->model('report_model');
                    $data['reportbydays'] = $this->report_model->get_report_day();
                    $this->load->model('chatbox_model');
                    $config['base_url'] = site_url('webhome/');
                    $data['chatbox'] = $this->chatbox_model->_showchatbox($config['per_page'], $this->uri->segment(2));
                   
                    $this->load->model('user_model');
                    $data['adminname'] = $this->session->userdata('adminid_sms');
            $data['adminid'] = $this->session->userdata('adminid_sms');
                    $config['base_url'] = site_url('cpanel/usercp');
                    $config['prev_link'] = 'Đầu';
                    $config['next_link'] = 'Tiếp';
                    $this->pagination->initialize($config);
                    $data['listusers'] = $this->user_model->_listuser($config['per_page'], $this->uri->segment(3));
                    $data['totaluser'] = $this->user_model->totaluser();
                    $this->load->model('transms_model');
                    $data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
                }
            } else {
                date_default_timezone_set('Asia/Bangkok');
                $config['base_url'] = site_url('cpanel/checkmoservices');
                $config['total_rows'] = $this->transms_model->totalmo();
                $config['per_page'] = 50;
                $config['prev_link'] = 'Đầu';
                $config['next_link'] = 'Tiếp';
                $this->pagination->initialize($config);
                $data['receivesmo'] = $this->transms_model->show_mo_sum($config['per_page'], $this->uri->segment(3));
                $data['pages'] = $this->pagination->create_links();
            }

            $this->load->view('appstore/admin/sms/dashboard', $data);
        }
    }

    public function servicescontrol() {

        if ($this->session->userdata('adminid_sms') == null) {
            redirect('cpanel/login');
        } else {
            
                $this->load->model('service_model');
                //--------------BEGIN Function ----------------------------------
                $this->load->model("transms_model");
                $this->load->model('report_model');
                $this->load->model('chatbox_model');
                $this->load->model('user_model');
                $this->load->model('transms_model');
                $data['totaluser'] = $this->user_model->totaluser();
                $data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
                $data['totalsms'] = $this->transms_model->totalmo();
                //--------------END Function ----------------------------------	

                if (isset($_REQUEST['submitservices'])) {
                    $shortcode = $this->input->post('shortcode', true);
                    $prefix_1 = $this->input->post('prefix_1', true);
                    $prefix_2 = $this->input->post('prefix_2', true);
                    $partner = $this->input->post('partner', true);
                    $apptype = $this->input->post('typeapp', true);
                    $return_value = $this->input->post('return_value', true);
                    $this->service_model->add_services($shortcode, $prefix_1, $prefix_2, $return_value, $partner, $apptype);
                    redirect('cpanel/servicescontrol');
                }

                $data['services'] = $this->service_model->show_services();
                $data['shoutbox'] = 0;
                $data['adminname'] = $this->session->userdata('adminid_sms');
                $data['adminid'] = $this->session->userdata('adminid');
              $this->load->view('appstore/admin/sms/dashboard', $data);
            
        }
    }
    
    public function usercp(){
		
		if($this->session->userdata('adminid_sms') == null){
			redirect('cpanel/login');
		}else{
			 
			$this->load->model("transms_model");
			$this->load->model('report_model');
			$this->load->model('chatbox_model');
			$this->load->model('user_model');
			$this->load->model('transms_model');
			$data['totaluser'] = $this->user_model->totaluser();
			$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
			$data['totalsms'] = $this->transms_model->totalmo();
			//--------------END Function ----------------------------------	
			$this->load->model('user_model');
			$data['adminname'] = $this->session->userdata('adminid_sms');
			$data['adminid'] = $this->session->userdata('adminid');
			$data['shoutbox'] =0;
			$config['base_url'] = site_url('cpanel/usercp');
			$config['total_rows'] = $this->user_model->totaluser();
			$config['per_page'] = 10;
			$config['prev_link'] = 'Đầu';
			$config['next_link'] = 'Tiếp';
			$this->pagination->initialize($config);
			$data['listusers'] = $this->user_model->_listuser($config['per_page'],$this->uri->segment(3));
			$data['pages'] = $this->pagination->create_links();
			$data['totaluser'] = $this->user_model->totaluser();
			$this->load->view('appstore/admin/sms/dashboard', $data);
			 
		}
	}

}

?>
